package com.yixun.qfbao.controller.agency.weapp;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.yixun.qfbao.api.service.HomeService;
import com.yixun.qfbao.api.service.login.BaseLoginService;
import com.yixun.qfbao.api.service.login.LoginFactory;
import com.yixun.qfbao.apidto.base.CommonReqDTO;
import com.yixun.qfbao.controller.LoginController;
import com.yixun.qfbao.request.BaseRequest;
import com.yixun.qfbao.request.home.GetPartnerInfoReqDto;
import com.yixun.qfbao.response.BaseResponse;
import com.yixun.qfbao.response.home.AttetionResDto;
import com.yixun.qfbao.response.login.PartnerInfoListResponse;
import com.yixun.qfbao.xf.model.AttetionInfo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zilong.liu
 * @date 2020-10-21
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/agency/weapp/home")
@Api(tags = {"分销小程序-首页"})
public class HomeController {

	@Autowired
	HomeService homeService;
	
	@Autowired
	LoginController loginController;
	
	@ApiOperation(notes = "首页", value="获取关注状态")
    @PostMapping("/getAttetionInfo")
    public BaseResponse<AttetionResDto> getAttetionInfo(@RequestBody @Valid CommonReqDTO request) {
		
		AttetionInfo attetionInfo = homeService.getAttetionInfo(request.getLoginType(), 
				request.getUserId(), request.getCompanyUuid());
		
		BaseResponse<AttetionResDto> response = new BaseResponse<>();
		response.setCode(attetionInfo.getResCode());
		response.setMsg(attetionInfo.getResMsg());
		AttetionResDto attetionResDto = new AttetionResDto();
		try {
			BeanUtils.copyProperties(attetionResDto, attetionInfo);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		response.setData(attetionResDto);
		return response;
	}
	
	@ApiOperation(notes = "首页", value="根据手机号获取所有商户人员")
    @PostMapping("/getAllPartnerByPhone")
    public BaseResponse<AttetionResDto> getAllPartnerByPhone(@RequestBody @Valid CommonReqDTO request) {
		
		AttetionInfo attetionInfo = homeService.getAttetionInfo(request.getLoginType(), 
				request.getUserId(), request.getCompanyUuid());
		
		BaseResponse<AttetionResDto> response = new BaseResponse<>();
		response.setCode(attetionInfo.getResCode());
		response.setMsg(attetionInfo.getResMsg());
		AttetionResDto attetionResDto = new AttetionResDto();
		try {
			BeanUtils.copyProperties(attetionResDto, attetionInfo);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		response.setData(attetionResDto);
		return response;
	}

	@ApiOperation(notes = "首页", value="根据手机号码获取所有的分销人员信息")
    @PostMapping("/getPartnerInfo")
    public BaseResponse<PartnerInfoListResponse> getPartnerInfo(@RequestBody @Valid BaseRequest<GetPartnerInfoReqDto> request) {
		
		BaseResponse<PartnerInfoListResponse> response = new BaseResponse<>();
		
		BaseLoginService loginService = LoginFactory.getLoginService(request.getLoginType());
		
		//获取可以查询的companyUuid
		List<String> companyUuids = loginService.getAllCompanyUuid(request.getCompanyUuids(), request.getLoginType());
		//获取该账号对应的所有用户
		List<Map<String, Object>> partnerInfos = loginService.getPartnerInfosByPhone(request.getData().getPhone(),
				0L, null, 1, companyUuids);
		
		if(partnerInfos == null) {
			response.setCode(ResultEnum.FAIL.getNo());
			response.setMsg("未查询到当前账号对应的信息");
			return response;
		}
		
		PartnerInfoListResponse partnerInfoListResponse = new PartnerInfoListResponse();
		partnerInfoListResponse.setPartnerInfoListResponse(loginController.buildLoginResponse(request, partnerInfos));
		partnerInfoListResponse.getPartnerInfoListResponse().stream().
			forEach(partnerInfoResponse-> partnerInfoResponse.setOpenId(request.getData().getOpenId()));
		response.setData(partnerInfoListResponse);
		return response;
	}
}
